/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.phoenix.repository.dao.mysql;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Value;

import net.phoenix.repository.dao.ValueTableUpdateDao;

/**
 * Jigsaw server
 * 
 * @author <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date 2012-11-19
 * @version 1.0.0
 */
public class MysqlValueTableUpdateDao extends ValueTableUpdateDao {

	private List<Object> parameters;
	private StringBuffer sql; // columns part of sql;

	public MysqlValueTableUpdateDao() {
		this.parameters = new ArrayList<Object>();
		this.sql = new StringBuffer();
	}

	@Override
	public void removeValue(String column, boolean multiple) {
		int count = 1;
		if (multiple)
			count = 8;
		for (int i = 0; i < count; i++) {
			this.removeValue(column, i);
		}
	}

	public void removeValue(String column, int index) {
		if (sql.length() > 0)
			sql.append(",");
		sql.append("`");
		sql.append(column);
		sql.append("[" + index + "]`= NULL");

	}

	@Override
	public void setColumnValue(String column, Value value, int type) throws RepositoryException {
		this.setColumnValue(column, value, type, 0);

	}

	@Override
	public void setColumnValues(String column, Value[] values, int type) throws RepositoryException {
		if (values == null)
			this.removeValue(column, true);
		else {
			int i = 0;
			for (i = 0; i < values.length; i++) {
				this.setColumnValue(column, values[i], type, i);
			}
			for (; i < 8; i++) {
				this.removeValue(column, i);
			}
		}

	}

	private void setColumnValue(String column, Value value, int type, int index) throws RepositoryException {
		if (value == null)
			this.removeValue(column, index);
		else {
			switch (type) {
			case PropertyType.BOOLEAN:
				this.parameters.add(value.getBoolean());
				break;
			case PropertyType.DATE:
				this.parameters.add(new Timestamp(value.getDate().getTimeInMillis()));
				break;
			case PropertyType.DOUBLE:
				this.parameters.add(value.getDouble());
				break;
			case PropertyType.LONG:
				this.parameters.add(value.getLong());
				break;
			default:
				this.parameters.add(value.getString());
			}
			if (sql.length() > 0)
				sql.append(",");			
			sql.append("`");
			sql.append(column);
			sql.append("[" + index + "]`=? ");
		
		}

	}

	@Override
	public void update(String table, String nodeId) throws SQLException {
		if(this.sql.toString().trim().length()==0)
			return;
		StringBuffer sql = new StringBuffer("UPDATE `" + table + "` SET ");
		sql.append(this.sql);
		sql.append(" WHERE PKID=?");
		PreparedStatement statement = this.connection.prepareStatement(sql.toString());
		int i = 0;
		for (i = 0; i < this.parameters.size(); i++) {
			statement.setObject(i + 1, this.parameters.get(i));
		}
		statement.setObject(i + 1, nodeId);
		statement.executeUpdate();
		statement.close();

	}

}
